package com.google.code.b0rx0r;

import java.awt.BorderLayout;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import jvst.wrapper.VSTPluginAdapter;
import jvst.wrapper.VSTPluginGUIAdapter;
import jvst.wrapper.gui.VSTPluginGUIRunner;
import net.beeger.squareness.SquarenessLookAndFeel;
import net.beeger.squareness.theme.PropertiesSquarenessTheme;
import net.beeger.squareness.theme.PropertiesSquarenessTheme.LoadException;

import org.jdesktop.swingx.icon.EmptyIcon;

import com.google.code.b0rx0r.program.ProgramManager;
import com.google.code.b0rx0r.ui.MainPane;
import com.google.code.b0rx0r.ui.Splash;

/** Dont forget to mention the icons we took from 
 *  http://www.brandspankingnew.net!
 *  "A couple of people have been asking about licensing. The downloads on this site, unless otherwise stated, are covered by a Creative Commons Attribution-ShareAlike 2.5 licence. This means that you can download the icons and use them for whatever you like, including commercial work. You can even redistribute them, or modify them. All I ask is that you include a credit ("attribution") and if possible a back link to my site."
 *
 * @author Lui
 *
 */
public class B0rx0rGui extends VSTPluginGUIAdapter {

	private B0rx0r main;
	private ProgramManager programManager;
	
	{
		try {
			Properties p = new Properties();
			p.load(getClass().getClassLoader().getResourceAsStream("/b0rx0r.theme")); //$NON-NLS-1$
			
			SquarenessLookAndFeel laf = new SquarenessLookAndFeel();
			laf.setCurrentSquarenessTheme(new PropertiesSquarenessTheme(p));
			
			UIManager.setLookAndFeel(laf);
			
			ColorScheme.update(laf);
			
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LoadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	public B0rx0rGui(VSTPluginGUIRunner r, VSTPluginAdapter main) {
		super(r, main);
		
		this.main = (B0rx0r) main;

		new Splash().display(5);
		
		if (this.main != null) {
			programManager = this.main.getProgramManager();
		} else {
			//for testing only.
			programManager = new ProgramManager(new FixedTempoProvider(95));
		}
		
		initGui();
		
		setVisible(true);
	}

	private void initGui() {
		getContentPane().setLayout(new BorderLayout());
		PlayQueue pq = main == null ? new PlayQueue(programManager, null) : main.getQueue();
		MainPane mp = new MainPane(programManager, pq);
		getContentPane().add(mp, BorderLayout.CENTER);
		
		pack();
		
	}

	public static Icon createIcon(String filename) {
		URL url = B0rx0rGui.class.getResource("/mini_icons2/"+filename); //$NON-NLS-1$
		if (url == null)
			return new EmptyIcon();
		
		return new ImageIcon(url);
	}
	
	
	

}
